package jp.co.dailyReport.search.action;

import java.util.Calendar;
import java.util.Vector;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import jp.co.dailyReport.dailyReport.DailyReport;
import jp.co.dailyReport.dailyReport.DailyReportWorkItem;
import jp.co.dailyReport.dailyReport.DailyReportManager;
import jp.co.dailyReport.dailyReport.DailyReportWorkItemManager;
import jp.co.dailyReport.workItem.WorkItem;
import jp.co.dailyReport.workItem.WorkItemManager;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.staff.StaffManager;
import jp.co.dailyReport.project.Project;
import jp.co.dailyReport.project.ProjectManager;
import jp.co.dailyReport.DataUnavailableException;

/**
 * 日報検索の検索結果一覧ページの氏名のリンク
 * が押されたときの処理を管理するクラスです。
 */
public class SearchDailyReportInfoAction extends Action {

	private static Logger log = Logger.getLogger(SearchDailyReportInfoAction.class);

	/**
	 * 日報検索の検索結果一覧ページの氏名のリンクが押されたときの処理を実行します。<BR>
	 * 日報を書いた社員情報、作業項目情報、プロジェクト情報を取得します。<BR>
	 * 日報IDから日報情報を取得し、DynaActionFormオブジェクトのフィールドに設定します。
	 * 
	 * @param mapping このインスタンスを選択するために使用したActionMapping
	 * @param form 存在するならば、このリクエストのためのActionForm Bean
	 * @param request 処理しているHTTPリクエスト
	 * @param response 処理しているHTTPレスポンス
	 * @return ActionForwardインスタンス
	 * @throws IOException, ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
		 throws IOException, ServletException {

		/* 一覧から選択されたdailyReportIdのdailyReport情報を表示する */

		log.debug("SearchDailyReportInfoAction start");
		HttpSession session = request.getSession();
		log.debug("session.getId() = " + session.getId());
		request.removeAttribute("dailyReportWorkItem");
		request.removeAttribute("workItemList");
		request.removeAttribute("projectList");

		DynaActionForm searchDailyReportInfoActionForm = (DynaActionForm) form;
		DailyReport dailyReport = null;
		DailyReportWorkItem dailyReportWorkItems[] = null;
		WorkItem workItems[] = null;
		Project projects[] = null;
		WorkItem workItem = null;
		Staff staff = null;
		Vector workItemList = new Vector();

		try {
			Integer dailyReportId = (Integer)searchDailyReportInfoActionForm.get("dailyReportId");
			log.debug("Search info dailyReportId: " + dailyReportId);

			dailyReport = DailyReportManager.load(dailyReportId.intValue());
			int staffId = dailyReport.getStaffId();
			staff = StaffManager.load(staffId);
			dailyReportWorkItems = DailyReportWorkItemManager.load(dailyReportId.intValue());
			projects = ProjectManager.load();
			
			/* 指定されたdailyReportId->workItemId取得 */
			/* workItemIdでworkItem obj取得 */ 
			/* workItemのprojectId->Project obj取得 */
			for(int i=0; i < dailyReportWorkItems.length ; i++){
				int workItemId = dailyReportWorkItems[i].getWorkItemId();
				workItem = WorkItemManager.load(workItemId);
				workItemList.add(workItem);
			}


		} catch(DataUnavailableException due) {
			log.error(due.getMessage());
		}

		searchDailyReportInfoActionForm.set("staffId", new Integer(staff.getStaffId()));
		searchDailyReportInfoActionForm.set("name", staff.getName());
		searchDailyReportInfoActionForm.set("workDateY", new Integer(dailyReport.getWorkDate().get(Calendar.YEAR)));
		searchDailyReportInfoActionForm.set("workDateM", new Integer(dailyReport.getWorkDate().get(Calendar.MONTH)+1));
		searchDailyReportInfoActionForm.set("workDateD", new Integer(dailyReport.getWorkDate().get(Calendar.DATE)));

		searchDailyReportInfoActionForm.set("workingStartHoursH", new Integer(dailyReport.getWorkingStartHours().get(Calendar.HOUR_OF_DAY)));
		searchDailyReportInfoActionForm.set("workingStartHoursM", new Integer(dailyReport.getWorkingStartHours().get(Calendar.MINUTE)));
		searchDailyReportInfoActionForm.set("workingEndHoursH", new Integer(dailyReport.getWorkingEndHours().get(Calendar.HOUR_OF_DAY)));
		searchDailyReportInfoActionForm.set("workingEndHoursM", new Integer(dailyReport.getWorkingEndHours().get(Calendar.MINUTE)));
		searchDailyReportInfoActionForm.set("totalWorkingHours", new Double(dailyReport.getTotalWorkingHours()));

		searchDailyReportInfoActionForm.set("workDetail", dailyReport.getWorkDetail());
		searchDailyReportInfoActionForm.set("problem", dailyReport.getProblem());
		searchDailyReportInfoActionForm.set("toDo", dailyReport.getToDo());
		searchDailyReportInfoActionForm.set("comment", dailyReport.getComment());

		request.setAttribute("dailyReportWorkItem", dailyReportWorkItems);
		request.setAttribute("workItemList", WorkItemManager.sortWorkItemList(workItemList));
		request.setAttribute("projectList", projects);

		log.debug("SearchDailyReportInfoAction end");
		return mapping.findForward("info");
	}
}
